var app = getApp()
// pages/order_list/order_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: true,
    selected1: false,
    state: 0,
    order_list: [],
    curpage: 1,
    k: '',
    u: '',
    request_num: '',//加载底部使用
    show: 0,
    is_show_time: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var curpage = that.data.curpage
    if (options.state) {
      var state = options.state
    } else {
      var state = that.data.state
    }
    that.setData({
      k: key,
      u: uid,
    })

    if (key) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=index&typ=json&curpage=' + curpage,
        method: 'GET',
        data: { k: key, u: uid, state: that.data.state },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          that.setData({
            order_list: res.data.data.items,
            is_show_time: 0
          })
        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }
  },
  //订单列表切换
  order: function (e) {
    var that = this;
    var state = e.currentTarget.dataset.state
    var curpage = 1
    switch (state) {
      case '0':
        that.setData({
          selected: true,
          selected1: false,
        })
        break;
      case '2':
        that.setData({
          selected: false,
          selected1: true,
        })
        break;

    }
    if (that.data.k) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=index&typ=json&curpage=' + curpage,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, state: state },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          that.setData({
            order_list: res.data.data.items,
            state: state,
            curpage: curpage,
            request_num: '',
            is_show_time: 0
          })

        }
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.k) {
      var curpage = that.data.curpage + 1

      if (that.data.request_num == 1) {
        return false
      }

      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=index&typ=json&curpage=' + curpage,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, state: that.data.state },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          if (res.data.data.items.length > 0) {
            var order_list = that.data.order_list
            for (var z in res.data.data.items) {
              order_list.push(res.data.data.items[z])
            }
            that.setData({
              order_list: order_list,
              curpage: curpage,
              is_show_time: 0
            })
          } else {
            that.setData({
              request_num: 1,
              is_show_time: 0
            })
          }
        }
      })
    }
  },
  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },

})